package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.MyBean;
import com.zprmb.udzt.util.BitmapUtils;

public class Hongbao_Adapter extends BaseAdapter {
	private List<MyBean> list;
	private Context context;
	DecimalFormat df = new DecimalFormat("###0.00"); 
    private String title;
    
	
   
	public Hongbao_Adapter(List<MyBean> list, Context context, String title) {
		super();
		this.list = list;
		this.context = context;
		this.title = title;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Help h=null;
		if(convertView==null){
			h=new Help();
			try {
				
				convertView=LayoutInflater.from(context).inflate(R.layout.coupon_item, null);
			} catch (OutOfMemoryError e) {
				// TODO: handle exception
			}
			h.tv_name=(TextView)convertView.findViewById(R.id.name);
			h.tv_rate=(TextView)convertView.findViewById(R.id.rate);
//			h.tv_status=(TextView)convertView.findViewById(R.id.status);
			h.tv_time=(TextView)convertView.findViewById(R.id.end_time);
			h.li_bg=(LinearLayout)convertView.findViewById(R.id.li_bg);
			h.ck=(CheckBox)convertView.findViewById(R.id.ck);
			h.tv_use_ground=(TextView)convertView.findViewById(R.id.use_ground);
			h.li_ground=(LinearLayout)convertView.findViewById(R.id.li_ground);
//			h.ground=(TextView)convertView.findViewById(R.id.ground);
			h.baifen=(TextView)convertView.findViewById(R.id.baifen);
			h.img_get=(ImageView)convertView.findViewById(R.id.img_get);
//			h.coupon_status=(TextView)convertView.findViewById(R.id.coupon_status);
//			h.rl_red=(RelativeLayout)convertView.findViewById(R.id.rl_red);
			h.li_coupon=(LinearLayout)convertView.findViewById(R.id.li_coupon);
//			h.zhanwei=(View)convertView.findViewById(R.id.zhanwei)s
			convertView.setTag(h);
		}else{
			h=(Help) convertView.getTag();
		}
		
        h.img_get.setVisibility(View.GONE);				
		h.li_ground.setVisibility(View.VISIBLE);
		h.baifen.setVisibility(View.GONE);
		h.red_name.setText(list.get(position).getCardTypeName()+"( "+list.get(position).getAmount()+"元 )");		
		h.tv_name.setText("剩余面额(元)");
		h.zhanwei.setVisibility(View.VISIBLE);
		h.zhanwei2.setVisibility(View.VISIBLE);
		h.tv_rate.setText(df.format(Double.parseDouble(list.get(position).getAvailableAmount())));
		h.tv_use_ground.setText(list.get(position).getProductAllowName());
		h.tv_time.setText("有效期至:"+list.get(position).getEndDate());
		h.rl_red.setVisibility(View.VISIBLE);
		if(title.equals("可用")){
		h.rl_red.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.redtopbg)));
		h.tv_rate.setTextColor(Color.parseColor("#f84746"));
		h.red_name.setTextColor(Color.parseColor("#ffffff"));
		}
		else{
			h.rl_red.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.garytopbg)));
			h.tv_rate.setTextColor(Color.parseColor("#a8a8a8"));
			h.red_name.setTextColor(Color.parseColor("#323232"));
		}
		h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.redbg)));
//		h.li_bg.setBackgroundResource(R.drawable.redbg);
		if(list.get(position).getCashbackType()!=null&&list.get(position).getCashbackType().equals("PERCENTAGE")){
			h.tv_status.setText("返现比例:"+list.get(position).getGiftPercent()+"%"+"("+"已使用"+list.get(position).getUsedAmount()+"元)");	
		}else{
			h.tv_status.setText("返现比例:"+"年化"+list.get(position).getGiftPercent()+"%"+"("+"已使用"+list.get(position).getUsedAmount()+"元)");	
		}
		
		return convertView;
	}
	 class Help{
			private TextView tv_name,tv_status,tv_rate,tv_time,tv_use_ground,ground,baifen,coupon_status,red_name;
			private LinearLayout li_bg,li_ground,li_coupon;
			private View zhanwei,zhanwei2;
			private RelativeLayout rl_red;
			private ImageView img_get;
			private CheckBox ck;
		}

}
